<?php

namespace App\Services;

use App\Models\OrderAuditLog;
use App\Exceptions\ValidationException;
use App\Repositories\Contracts\MeetHourRepository;
use App\Repositories\Contracts\MeetOrderRepository;
use App\Repositories\Contracts\MeetPersonRepository;
use App\Repositories\Transformers\MeetOrderTransformer;

class MeetOrderService
{
    private $meetHourRepository;
    private $meetOrderRepository;
    private $meetPersonRepository;

    public function __construct(MeetOrderRepository $meetOrderRepository, MeetHourRepository $meetHourRepository
        , MeetPersonRepository $meetPersonRepository) {
        $this->meetOrderRepository = $meetOrderRepository;
        $this->meetHourRepository = $meetHourRepository;
        $this->meetPersonRepository = $meetPersonRepository;
    }

    /**
     * 获取列表
     *
     * @param array|null $param
     */
    public function getList($param = null)
    {
        try {
            $filter = [
                'add_uid' => $param['user_id'],
            ];
            if (isset($param['venue_id'])) {
                $filter['venue_id'] = $param['venue_id'];
            }
            $meetOrder = $this->meetOrderRepository->paginate($param['per_page'] ?? null, $filter);

            $result = fractal($meetOrder, new MeetOrderTransformer());

            return $result->toArray();

        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 获取详情
     *
     * @param array $param
     * @return object|array
     */
    public function getInfo($param)
    {
        try {
            $where = [
                'add_uid' => $param['user_id'],
            ];
            if (isset($param['venue_id'])) {
                $where['venue_id'] = $param['venue_id'];
            }

            $meetOrder = $this->meetOrderRepository->findByFilter($where);

            $result = fractal($meetOrder, new MeetOrderTransformer());

            return $result->toArray();
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 新增
     *
     * @param array $param
     */
    public function add($param)
    {
        try {

            foreach ($param['meet_person_id'] as $item) {
                $data['meet_hour_id'] = $param['meet_hour_id'];
                $data['venue_id'] = $param['venue_id'];
                $data['add_uid'] = $param['add_uid'];
                $data['meet_person_id'] = $item;

                // 场馆为1时，直接改成已审核，并插入审核日志表
                if ($param['venue_id'] == '1') {
                    $data['audit_status'] = 1;
                }

                // 新增关系
                $meetOrder = $this->meetOrderRepository->store($data);
                if ($param['venue_id'] == '1') {
                    // 插入审核日志表
                    $logData = [
                        'type' => 1,
                        'res_id' => $meetOrder->id,
                        'audit_user_id' => $meetOrder->add_uid,
                        'audit_status' => $meetOrder->audit_status,
                        'audit_admin_uid' => 1, //暂定编号为1的人员审核
                    ];
                    OrderAuditLog::create($logData);
                }
            }

            // 拼接返回
            $hour = $this->meetHourRepository->findByFilter([
                'meet_hour_id' => $param['meet_hour_id'],
            ]);
            $venueName = '';
            $meetDay = '';
            $durationTime = '';
            // 预约场馆
            if ($hour) {
                $venueName = $hour->venue->name;
                $meetDay = $hour->day->day;
                // 时间段
                $durationTime = $hour->start . '~' . $hour->end;
            }
            $persons = $this->meetPersonRepository->findInWhere('id', $param['meet_person_id'])->where('add_uid', $param['add_uid']);
            $meetPerson = [];
            if (count($persons) > 0) {

                foreach ($persons as $item) {
                    $temp = [];
                    $temp['name'] = $item->name;
                    $temp['paper_type'] = $item->paper_type;
                    $temp['paper'] = $item->paper;
                    array_push($meetPerson, $temp);
                }

            }
            return [
                'venue_name' => $venueName,
                'meet_day' => $meetDay,
                'duration_time' => $durationTime,
                'meet_person' => $meetPerson,
            ];
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 取消预约
     */
    public function cancel($param)
    {
        try {
            // 判断状态是否 未审核
            $meetOrder = $this->meetOrderRepository->find($param['id']);
            if (!empty($meetOrder) && $meetOrder->audit_status == 0) {
                $meetOrder = $this->meetOrderRepository->update([
                    'audit_status' => 3,
                ], $param['id']);
            } else {
                throw new ValidationException('状态不对');
            }
            return $meetOrder;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 修改
     *
     * @param array $param
     * @param int $id
     */
    public function update($param, $id)
    {
        try {
            $meetOrder = $this->meetOrderRepository->update($param, $id);
            return $meetOrder;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 删除
     *
     * @param int $id
     */
    public function delete($id)
    {
        try {
            $meetOrder = $this->meetOrderRepository->delete($id);

            $result = [];
            if ($meetOrder) {
                $result = 'success';
            } else {
                $result = 'error';
            }
            return $result;

        } catch (\Throwable $th) {
            throw $th;
        }
    }
}
